var loader

// 返回开发年份
function getYears(developmentYear) {
	const date = new Date()
	const newYear = developmentYear !== date.getFullYear() ? developmentYear + ' - ' + date.getFullYear() : developmentYear
	return newYear
}

$('#developmentYear').text(getYears(2025))

const captchaId = $('#xf_captcha').data('captcha-id') || null

const $randomCopy = $('#randomCopy')

$.ajax({
	type: 'GET',
	url: 'https://node.api.xfabe.com/api/wenan?type=json&textName=经典名言',
	success: function (response) {
		const { code, data } = response
		$randomCopy.text(code === 200 ? data.text : '加载失败')
	},
	error: function () {
		$randomCopy.text('加载失败')
	}
})

$(document).ajaxStart(function () {
	$('button:submit').html('登录中...').attr('disabled', true)
	loader = $('button:submit').lyearloading({
		opacity: 0.2,
		spinnerSize: 'nm'
	})
}).ajaxStop(function () {
	if (loader) {
		loader.destroy()
		$('button:submit').html('立即登录').attr('disabled', false)
	}
})

function getUrlParams() {
	const search = window.location.search.substring(1)
	const params = {}

	if (!search) return params

	search.split('&').forEach(param => {
		let [key, value] = param.split('=')
		key = safeDecode(key)
		value = typeof value === 'undefined' ? '' : safeDecode(value)

		// 合并重复参数为数组
		if (params.hasOwnProperty(key)) {
			params[key] = [].concat(params[key], value)
		} else {
			params[key] = value
		}
	})

	return params
}

function safeDecode(component) {
	try {
		return decodeURIComponent(component.replace(/\+/g, ' '))
	} catch {
		return component
	}
}

const params = getUrlParams()
const $loginKeyDiv = $('#loginKeyDiv')

if (params['adminKey']) {
	console.log(params['adminKey'])
	$('#adminKey').val(params['adminKey'])
	$loginKeyDiv.show()
} else {
	$loginKeyDiv.remove()
}

$('.signin-form').on('submit', function (event) {
	let that = this
	if ($(that)[0].checkValidity() === false) {
		event.preventDefault()
		event.stopPropagation()
		$(that).addClass('was-validated')
		return false
	}

	var $data = $(that).serialize()

	const postRequest = (result, queryString = null) => {
		$.post($(that).attr('action'), result, function (res) {
			if (res.code === 200) {
				$.notify({
					message: '登录成功，页面即将跳转~',
				}, {
					type: 'success',
					placement: {
						from: 'top',
						align: 'right'
					},
					z_index: 10800,
					delay: 1500,
					animate: {
						enter: 'animate__animated animate__fadeInUp',
						exit: 'animate__animated animate__fadeOutDown'
					}
				})
				setTimeout(function () {
					location.href = queryString ? './index.html' + queryString : './index.html'
				}, 1500)
			} else {
				$.notify({
					message: '登录失败，错误原因：' + res.msg,
				}, {
					type: 'danger',
					placement: {
						from: 'top',
						align: 'right'
					},
					z_index: 10800,
					delay: 1500,
					animate: {
						enter: 'animate__animated animate__shakeX',
						exit: 'animate__animated animate__fadeOutDown'
					}
				})
				$('#password').val('')
				$("#captcha").click()
			}
		}).fail(function () {
			$.notify({
				message: '服务器错误',
			}, {
				type: 'danger',
				placement: {
					from: 'top',
					align: 'right'
				},
				z_index: 10800,
				delay: 1500,
				animate: {
					enter: 'animate__animated animate__shakeX',
					exit: 'animate__animated animate__fadeOutDown'
				}
			})
		})
	}

	if (!captchaId) {
		postRequest($data)
		return false
	}

	initGeetest4({
		captchaId: captchaId,
		language: 'zho',
		product: 'bind'
	}, gt => {
		gt.appendTo('#xf_captcha')
			.onSuccess(() => {
				const msgData = gt.getValidate()
				const params = new URLSearchParams(msgData)
				const queryString = params.toString()

				const currentUrl = window.location.href
				const url = new URL(currentUrl)

				postRequest($data + '&' + queryString, url.search)
			})
		gt.showBox()
	})

	return false
})